<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="xbsj-labels" content="Earth示例">
    </meta>
    <title>分析-剖切</title>
    <!-- 0 引入js文件：XbsjEarth.js和vue.min.js -->
    <script src="../../XbsjEarth/XbsjEarth.js"></script>
    <script src="./scripts/vue.min.js"></script>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
        }

        .box span {
            display: block;
            margin-top: 10px;
        }

        .defultbtn {
            display: inline-block;
            text-align: center;
            min-width: 60px;
            height: 38px;
            padding: 0 10px;
            line-height: 38px;
            border-radius: 100px;
            border: 1px solid #C9C9C9;
            background-color: #FFF;
            color: #555;
            cursor: pointer;
        }

        .defultbtn:hover {
            background-color: #b3daf8;
        }

        .btnon {
            background-color: #1E9FFF;
            color: #fff;
            border: 1px solid #1E9FFF;
        }
    </style>
</head>

<body>
    <div id="vueApp" style="width: 100%; height: 100%; background: grey; position: relative;">
        <earth-comp></earth-comp>
    </div>

    <script>
        // 1 创建Earth的vue组件
        var EarthComp = {
            template: `
                <div style="width: 100%; height: 100%">
                    <div ref="earthContainer" style="width: 100%; height: 100%">
                    </div>
                    <div class="box" style="position: absolute; left: 18px; top: 18px; color: white; background: rgba(0, 0, 0, 0.6); padding: 20px; border-radius: 25px;min-width:200px; font-size:24px; font-family: 宋体;">
                        <div class="defultbtn" :class="{'btnon':positionEditing}"  @click="positionEditing = !positionEditing">平移</div>
                        <div class="defultbtn" style="margin-left:20px;" :class="{'btnon':rotationEditing}"  @click="rotationEditing = !rotationEditing">旋转</div><br/>
                        <span>经度：{{position[0] | numFilter}}°</span> 
                        <span>纬度：{{position[1] | numFilter}}°</span> 
                        <span>高度：{{position[2] | numFilter2}}m</span> 
                        <span>偏航角：{{rotation[0] | numFilter2}}°</span> 
                        <span>俯仰角：{{rotation[1] | numFilter2}}°</span> 
                        <span>翻滚角：{{rotation[2] | numFilter2}}°</span> 
                    </div>
                </div>
            `,
            data() {
                return {
                    positionEditing: false,
                    rotationEditing: false,
                    position: [0, 0, 0],
                    rotation: [0, 0, 0],
                    _earth: undefined, // 注意：Earth和Cesium的相关变量放在vue中，必须使用下划线作为前缀！
                };
            },
            // 1.1 资源创建
            mounted() {
                // 1.1.1 创建地球
                var earth = new XE.Earth(this.$refs.earthContainer);

                // 1.1.2 场景配置
                earth.sceneTree.root = {
                    "title": "项目1",
                    "isSelected": true,
                    "children": [
                        {
                            "ref": "tileset",
                            "czmObject": {
                                "xbsjType": "Tileset",
                                "xbsjGuid": "b4137394-d346-4434-a01e-5b2d3730ec9f",
                                "name": "三维瓦片1x",
                                "url": "//lab.earthsdk.com/model/f15b9e90ac2d11e99dbd8fd044883638/tileset.json",
                                "lightColor": null,
                                "specularEnvironmentMaps": null,
                                "xbsjPosition": [
                                    1.9016974701882112,
                                    0.5972325152147303,
                                    425.8641913624607
                                ],
                                "xbsjFlattenGuid": "93916e9b-82dd-4a56-b15e-27303b08e781",
                                "xbsjClippingPlanes": {
                                    "planes": [
                                        "9ab55d98-32ed-49ad-9004-1940f4903b68"
                                    ]
                                },
                                "skipLevelOfDetail": false
                            }
                        },
                        {
                            "ref": "clippingPlane",
                            "czmObject": {
                                "xbsjType": "ClippingPlane",
                                "xbsjGuid": "9ab55d98-32ed-49ad-9004-1940f4903b68",
                                "name": "裁剪面1",
                                "position": [
                                    1.9016970582304769,
                                    0.5972442199495571,
                                    426
                                ],
                                "showHelper": true,
                            }
                        },
                        {
                            "czmObject": {
                                "xbsjType": "Imagery",
                                "name": "谷歌影像",
                                "xbsjImageryProvider": {
                                    "XbsjImageryProvider": {
                                        "url": "//www.google.cn/maps/vt?lyrs=s,h&gl=CN&x={x}&y={y}&z={z}",
                                        "srcCoordType": "GCJ02",
                                        "dstCoordType": "WGS84",
                                        "maximumLevel": 21,
                                    },
                                }
                            }
                        }
                    ]
                };

                var tileset = earth.sceneTree.$refs.tileset.czmObject;
                tileset.flyTo();

                // only for Debug
                window.earth = earth;
                window.tileset = tileset;
                window.clippingPlane = earth.sceneTree.$refs.clippingPlane.czmObject;

                // 1.1.5 数据绑定
                // disposers 用来收集资源销毁函数，并在析构时自动调用！
                {
                    this._disposers = [];
                    this._disposers.push(XE.MVVM.bind(this, 'positionEditing', clippingPlane, 'positionEditing'));
                    this._disposers.push(XE.MVVM.bind(this, 'rotationEditing', clippingPlane, 'rotationEditing'));
                    this._disposers.push(XE.MVVM.bindPosition(this, 'position', clippingPlane, 'position'));
                    this._disposers.push(XE.MVVM.bindRotation(this, 'rotation', clippingPlane, 'rotation'));
                }

                this.positionEditing = true; // 默认状态开启位置编辑

                this._earth = earth;
            },
            filters: {
                numFilter(value) {
                    // 截取当前数据到小数点后五位
                    let realVal = parseFloat(value).toFixed(5)
                    return realVal
                },
                numFilter2(value) {
                    // 截取当前数据到小数点后两位
                    let realVal = parseFloat(value).toFixed(2)
                    return realVal
                }
            },
            // 1.2 资源销毁
            beforeDestroy() {
                // vue程序销毁时，需要清理相关资源
                this._earth = this._earth && this._earth.destroy();
                this._disposers.forEach(d => d());
                this._disposers.length = 0;
            },
        }

        // 2 创建vue程序
        // XE.ready()用来加载Cesium.js等相关资源
        XE.ready().then(() => {
            var app = new Vue({
                el: '#vueApp',
                components: {
                    EarthComp,
                },
            });
        });
    </script>
</body>

</html>